using UnityEngine;
using UnityEngine.UI;

//敌人血条
public class EnemyHealthBar : MonoBehaviour
{
    public Image HP;        // 即时血量UI
    public Image slowHP;    // 缓冲血量UI

    private void Start() {
        HP.fillAmount = 1;
        gameObject.SetActive(false);
    }

    //更新血条UI
    public void UpdateHP(float currentHP, float maxHP)
    {
        gameObject.SetActive(true);
        HP.fillAmount = currentHP / maxHP;
    }

    private void Update()
    {
        // 当缓冲血量大于即时血条时，缓冲血条持续减少
        if (slowHP.fillAmount > HP.fillAmount)
        {
            // 缓冲血量持续减少，Time.daltaTime可换成其他时间参数，如0.01f
            slowHP.fillAmount -= Time.deltaTime * 0.5f;
        }

        if(slowHP.fillAmount < HP.fillAmount)
        {
            // 使缓冲血量与即时血量相等
            slowHP.fillAmount = HP.fillAmount;
        }

        if(slowHP.fillAmount == 0){
            gameObject.SetActive(false);
        }
    }
}

